package com.newbie.pet.controller;

import com.newbie.pet.model.BaseEntity;
import com.newbie.pet.service.BaseService;
import com.newbie.pet.wrap.DPage;
import com.newbie.pet.wrap.P;
import com.newbie.pet.wrap.R;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

/**
 * 类描述：控制器抽象类
 *
 * @author zhangkailiang
 * @date 2020/5/23
 */
public abstract class BaseController<S extends BaseService, E extends BaseEntity, ID> {
    @Autowired
    private S s;

    @ApiOperation(value = "保存信息", notes = "保存信息")
    @PostMapping("/save")
    public R<Boolean> save(@RequestBody E e) {
        return R.ok(s.save(e).getId() > 0);
    }

    @ApiOperation(value = "根据ID修改信息", notes = "根据ID修改信息")
    @PostMapping("/update")
    public R<Boolean> update(@Validated @RequestBody E e) {
        return R.ok(s.updateById(e));
    }

    @ApiOperation(value = "根据ID删除信息", notes = "根据ID删除信息")
    @PostMapping("/delete")
    public R<Boolean> delete(@RequestBody ID id) {
        return R.ok(s.deleteById(id));
    }

    @ApiOperation(value = "分页查询", notes = "分页查询")
    @PostMapping("/find/page")
    public R<DPage<E>> findPage(@RequestBody P<E> p) {
        return R.ok(s.findPage(p));
    }

    @ApiOperation(value = "根据ID查询信息", notes = "根据ID查询信息")
    @PostMapping("/find/one")
    public R<E> findOneById(@RequestBody ID id) {
        return R.ok((E) s.findOneById(id));
    }
}
